package com.video.main_video.manager.handle.register;

import com.video.main_video.dao.entity.User;
import com.video.main_video.dao.request.register.RegisterRequest;
import com.video.main_video.dao.response.register.RegisterResponse;
import com.video.main_video.manager.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
@Transactional
public class RegisterManager {
    @Autowired
    UserMapper userMapper;

    public RegisterResponse handler(RegisterRequest registerRequest) {
        System.out.println("注册账号");
        boolean registerResult = registerAccount(registerRequest);
        RegisterResponse res=new RegisterResponse();
        if(!registerResult){
            res.setCode(203);
            res.setMsg("用户名已经存在");
            return res;
        }
        res.setCode(200);
        res.setMsg("注册成功");
        return res;
    }

    private boolean registerAccount(RegisterRequest user){

//        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<User>();
//        qw.eq(User::getUserName,user.getUserName()).eq(User::getPassword,user.getPassword());
        User userInfo=new User();
        BeanUtils.copyProperties(user,userInfo);
        System.out.println(userInfo.toString());
        userInfo.setUserId(UUID.randomUUID().toString().replace("-",""));
        try{
            int insert = userMapper.insert(userInfo);
            return insert != 0;
        }catch (Exception e){

        }

        return false;
    }

}
